/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*!
 * @file rtc_reg_access.c
 * @brief This file defines rtc register access functions
 */

/*******Includes***************************************************************/
#include "rtc_reg_access.h"

/*******Definitions************************************************************/

/*******Variables**************************************************************/

/*******Prototypes*************************************************************/

/*******Private Functions******************************************************/

/*******Code*******************************************************************/
/*******************************************************************************
 *** Function Name : RTC_REG_SetRTCEnable
 *** Description   : Enable or disable the RTC
 ******************************************************************************/
void RTC_REG_SetRTCEnable(RTC_Type* regBase, bool enable)
{
    uint32_t val = regBase->CFG;

    val &= ~(RTC_CFG_RTCEN_Msk);
    val |= RTC_CFG_RTCEN(enable ? 1U : 0U);
    regBase->CFG = val;
}

/*******************************************************************************
 *** Function Name : RTC_REG_SetCounterValue
 *** Description   : Set RTC counter value, the counter can only be set when RTC
 ***                 is disabled
 ******************************************************************************/
errcode_t RTC_REG_SetCounterValue(RTC_Type* regBase, uint32_t value)
{
    errcode_t errCode = ERR_SUCCESS;

    if (RTC_REG_IsRTCEnabled(regBase) == true) {
        errCode = ERR_BUSY;
    } else {
        regBase->SCNT = value;
        /* Wait for counter value write done */
        while (((regBase->STATUS & RTC_STATUS_SCNTWF_Msk) >> RTC_STATUS_SCNTWF_Pos) == 0U);
        /* Clear counter value write flag */
        regBase->STATUS = ((uint32_t)1U << RTC_STATUS_SCNTWF_Pos) & RTC_STATUS_SCNTWF_Msk;
    }

    return errCode;
}

/*******************************************************************************
 *** Function Name : RTC_REG_SetPrescalerValue
 *** Description   : Set RTC prescaler value, the prescaler can only be set when RTC
 ***                 is disabled
 ******************************************************************************/
errcode_t RTC_REG_SetPrescalerValue(RTC_Type* regBase, uint16_t value)
{
    errcode_t errCode = ERR_SUCCESS;

    if (RTC_REG_IsRTCEnabled(regBase) == true) {
        errCode = ERR_BUSY;
    } else {
        regBase->PSCAL = value;
    }

    return errCode;
}

/*******EOF********************************************************************/
